package LeetCode热题HOT100;

import 剑指offer.ListNode;

public class p2两数相加 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode pHead=new ListNode();//傀儡结点
        ListNode cur=pHead;
        int carry=0;//记录进位

        while (l1!=null || l2!=null) {
            int one= (l1==null) ? 0 : l1.val;
            int two= (l2==null) ? 0 : l2.val;

            int sum=one+two+carry;
            cur.next=new ListNode(sum%10);
            carry=sum/10;
            cur=cur.next;

            if (l1!=null) {
                l1=l1.next;
            }

            if (l2!=null) {
                l2=l2.next;
            }

        }

        if (carry==1){
            cur.next=new ListNode(carry);
        }
        return pHead.next;


    }


}
